// This is JDefenceTower, a simple tower defence style game
//
// Copyright (C) 2010 Maximilian Junker
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, see <http://www.gnu.org/licenses/>.

package junker.defence.ui;

import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.image.BufferStrategy;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;

import junker.defence.model.CreateRequest;
import junker.defence.model.CreationPattern;
import junker.defence.model.Game;
import junker.defence.model.IMapEventListener;
import junker.defence.model.Map;
import junker.defence.model.MapObject;
import junker.defence.model.MapObjectDescription;

@SuppressWarnings("serial")
public class MapUI extends Canvas implements IMapEventListener{
	private Map				map;
	private List<Sprite>	sprites	= new LinkedList<Sprite>();
	private BufferStrategy	strategy;
	private static final Logger logger = Logger.getLogger(MapUI.class.getName());
	private static final Logger inputLogger = Logger.getLogger(MapUI.class.getName() + ".input");
	
	public MapUI(Map map) {
		super();
		this.map = map;
		map.addMapEventListener(this);
	}

	@Override
	public void paint(Graphics g) {
		draw();
	}
	
	@Override
	public void update(Graphics g) {
		paint(g);
	}

	public void draw() {
		try{
			logger.log(Level.FINEST,"Drawing");
			Graphics2D g = (Graphics2D)getStrategy().getDrawGraphics();
			g.transform(AffineTransform.getScaleInstance(UIConfig.SCALE, UIConfig.SCALE));
			g.clearRect(0, 0, getWidth(), getHeight());
			drawBackground(g);
			for (Sprite sprite : sprites) {
				sprite.draw(g);
			}
			this.getBufferStrategy().show();
			Toolkit.getDefaultToolkit().sync();
		} catch(Exception e){
			logger.log(Level.WARNING,"Exception occured during drawing");
		}
	}

	private void drawBackground(Graphics2D g){
		g.setStroke(new BasicStroke(0.1f));
		g.setColor(Color.gray);
		for(int i = 0;i<map.getSizeX()+1;i+=1){
			g.draw(new Line2D.Float(i,0,i,map.getSizeY()+1));
		}
		for(int i = 0;i<map.getSizeY()+1;i+=1){
			g.drawLine(0, i, map.getSizeX()+1,i);
		}
	}
	
	private BufferStrategy getStrategy() {
		BufferStrategy strategy = getBufferStrategy();
		if(strategy==null){
			createBufferStrategy(2);
			strategy=getBufferStrategy();
		}
		return strategy;
	}

	@Override
	public void objectCreated(MapObject o) {
		logger.log(Level.FINEST,"Created");
		sprites.add(SpriteFactory.createSprite(o));
	}

}
